#include "stdafx.h"
#include "../Engine/World.hpp"
#include "../Engine/WorldRenderer.hpp"

namespace he
{
	WorldRenderer::WorldRenderer(Ogre::SceneManager &SceneManager, World const &World_)
		: m_SceneManager(SceneManager),
		m_World(World_),
		m_MapRenderer(&m_World.map(), m_World.terrain_material_manager(), m_SceneManager),
		m_BuildingRenderer(m_World.map(), m_MapRenderer, m_SceneManager,World_.building_material_manager()),
		m_UnitEntityManager(boost::filesystem::path(), m_SceneManager)
	{
		// Set shadowing
		std::unique_ptr<Ogre::LiSPSMShadowCameraSetup> p_camera_setup(new Ogre::LiSPSMShadowCameraSetup());
		p_camera_setup->setOptimalAdjustFactor(0.8f);
		p_camera_setup->setUseAggressiveFocusRegion(true);

		m_SceneManager.setShadowTechnique(Ogre::SHADOWTYPE_TEXTURE_ADDITIVE);
		m_SceneManager.setShadowFarDistance(50);
		m_SceneManager.setShadowTextureSize(1024);
		m_SceneManager.setShadowCameraSetup(Ogre::ShadowCameraSetupPtr(p_camera_setup.release()));
		// Create map model
		m_MapRenderer.create_map_model();
		// Create buildings
		BuildingVector_t const &buildings = m_World.buildings();
		for(auto it = buildings.begin(), end = buildings.end(); it != end; ++it)
			m_BuildingRenderer.add(**it);
		m_Physics.associate(&m_MapRenderer);
	}

	MapRenderer &WorldRenderer::map_renderer()
	{
		return m_MapRenderer;
	}

	void WorldRenderer::update(float Time)
	{
		// Update physics
		m_Physics.update(Time);
	}
} /*he*/
